/**
 * 事件驱动器
 */
!function(){
    dispatcher = {
        events: {},
        // 监听事件
        listen: function (event, callback) {
            if(!this.events[event]) {
                this.events[event] = []
            }
            this.events[event].push(callback)
        },
        // 触发事件
        trigger: function(event, options) {
            let callbacks = this.events[event];
            if(!!callbacks) {
                for (let i = 0, j = callbacks.length; i < j; i++) {
                    callbacks[i](options)
                }
            }
        }
    };
}();

/**
 * 消息函数
 */
!function () {
    $.ajaxSettings = $.extend($.ajaxSettings, {
        complete: function (xhr) {
            if(xhr.status === 202) {
                let response = xhr.responseText;
                if(response.indexOf("MsgType")>0) {
                    console.log("测试 index===0 的情况");
                    let msg = JSON.parse(xhr.responseText);
                    msg_alert[msg["MsgType"]](msg["MsgContent"]);
                }
            }
        }
    });

    msg_alert = {
        warning: function (msg) {
            dispatcher.trigger("message", {
                typ:"warning",
                content:msg,
                hidden:false,
            })
        },
        info: function (msg) {
            dispatcher.trigger("message", {
                typ:"info",
                content:msg,
                hidden:false,
            })
        },
        success: function (msg) {
            dispatcher.trigger("message", {
                typ:"success",
                content:msg,
                hidden:false,
            })
        },
        danger: function (msg) {
            dispatcher.trigger("message", {
                typ:"danger",
                content:msg,
                hidden:false,
            })
        },
    };
}();

/**
 * 获取 URL 参数
 */
!function () {
    getQuery = function (variable) {
        var query = window.location.search.substring(1);
        var vars = query.split('&');
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split('=');
            if (decodeURIComponent(pair[0]) == variable) {
                return decodeURIComponent(pair[1]);
            }
        }
    };
}();

/**
 * user_info() 获得登陆用户信息
 */
!function () {
    let info = null;
    user_info = function () {
        if(!info) {
            $.ajax({
                url:"/admin/info",
                type:"GET",
                async: false,
                success: function(data){
                    if(!!data) {
                        info = data;
                    }
                }
            })
        }
        return info;
    }
}();